#include "componentwidget.h"
#include "itemwidget.h"
#include <QListWidget>
#include <QListWidgetItem>
#include <QScrollBar>
#include <QVBoxLayout>

ComponentWidget::ComponentWidget(QScrollArea *parent)
    : QScrollArea(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);

    setMaximumSize((Screen::GetScreenWidth()>>1), (Screen::GetScreenHeight()));
    setMinimumSize((Screen::GetScreenWidth())>>5, (Screen::GetScreenHeight())>>4);
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    setFrameShape(QScrollArea::NoFrame);
    setWidgetResizable(true);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    list_widget = new QListWidget(this);
    list_widget->setFrameShape(QListView::NoFrame);
    list_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    list_widget->setFixedHeight(Screen::GetScreenHeight()>>1);
    list_widget->setItemDelegate(new NoFocusDelegate());
    list_widget->setViewMode(QListView::ListMode);
    list_widget->setFocusPolicy(Qt::NoFocus);
    list_widget->setResizeMode(QListWidget::Adjust);
    list_widget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    list_widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    for (int i = 0; i < myapp::object_num; i++)
    {
        if (s_object[i].object_name == "EtaView" || s_object[i].object_name == "StationView"
                || s_object[i].object_name == "LineView" || s_object[i].object_name == "PointView")
            continue;
        QListWidgetItem *item = new QListWidgetItem(list_widget);
        item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
        item->setSizeHint(QSize(Screen::GetScreenWidth()>>5, 50));

        ItemWidget *item_widget = new ItemWidget(s_object[i], this);
        list_widget->setItemWidget(item, item_widget);
        connect(item_widget, &ItemWidget::mouseRelease, this, &ComponentWidget::SltMouseRelease);
    }
    setWidget(list_widget);
    list_widget->setVisible(false);
}

ComponentWidget::~ComponentWidget()
{

}

void ComponentWidget::show_list_widget()
{
    list_widget->setVisible(true);
}


void ComponentWidget::SltMouseDoubleClick(QString name)
{
    LOG_QDEBUG<<"slt____"<<name;
}

void ComponentWidget::SltMouseRelease(ObjectInfo param, QPoint point)
{
    emit sgl_to_mainwindow_for_create_item(param, point);
}


